package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    //潜在客户
    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"customer:potentialList", "潜在客户列表"},logical = Logical.OR)
    public String potentialList(Model model,@ModelAttribute("qo")CustomerQuery qo){
        //只查询潜在客户
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result",pageInfo);
        List<Employee> employees = employeeService.selectEmpByRole("Market_Manager","Market");
        model.addAttribute("employees",employees);
        //客户职业
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByDicSn("job");
        model.addAttribute("jobs",jobs);
        //客户来源
        List<SystemDictionaryItem> source = systemDictionaryItemService.selectByDicSn("source");
        model.addAttribute("source",source);
        //客户来源
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByDicSn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "/customer/potentialList";
    }

    //客户管理页面
    @RequestMapping("/list")
    @RequiresPermissions(value = {"customer:list", "客户管理查询"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo")CustomerQuery qo){
        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result",pageInfo);
        //销售人员
        List<Employee> employees = employeeService.selectEmpByRole("Market_Manager", "Market");
        model.addAttribute("employees",employees);
        return "/customer/list";
    }


    //删除
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"customer:delete", "客户删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        customerService.deleteByPrimaryKey(id);
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"customer:saveOrupdate", "客户新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrupdate(Customer customer){

            if (customer.getId() == null) {
                customerService.save(customer);
            }else{
                customerService.updateByPrimaryKey(customer);
            }
            return new JsonResult();

    }

    //检查电话是否重复
    @RequestMapping("checkTel")
    @ResponseBody
    public Map<String,Boolean> checkTel(String tel,Long id){
        Map<String, Boolean> map = new HashMap<>();
        //如果id为空,就是新增
        if(id == null){
            //新增只需要查询电话是否重复即可
            String checkTel = customerService.checkTel(tel);
            map.put("valid",checkTel==null);
            return map;
        }else{
            //判断这个电话跟这个id里面对应的电话是否一致
            Customer customer = customerService.checkTelById(tel,id);
            // 一致就通过
            if(customer != null){
                map.put("valid",true);
                return map;
            }else {
                //不一致就判断是否有重复
                String checkTel = customerService.checkTel(tel);
                map.put("valid",checkTel==null);
                return map;
            }
        }
    }

    //客户池
    @RequestMapping("/poolList")
    @RequiresPermissions(value = {"customer:poolList", "客户池"},logical = Logical.OR)
    public String poolList(Model model,@ModelAttribute("qo")CustomerQuery qo){
        PageInfo pageInfo = customerService.queryPool(qo);
        model.addAttribute("result",pageInfo);
        //销售人员
        List<Employee> employees = employeeService.selectEmpByRole("Market_Manager", "Market");
        model.addAttribute("employees",employees);
        return "/customer/poolList";
    }
}
